package com.asia5b.a5mall.order.adapter

import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.StringUtils
import com.asia5b.a5mall.middleware.utils.startGoodsDetailActivity
import com.asia5b.a5mall.middleware.utils.startShopActivity
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.activity.OrderDetail2Activity
import com.asia5b.a5mall.order.databinding.ItemOrderdetailShop3adapterBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/5/9.
 */
class OrderDetailShop3Adapter(var list: ArrayList<OrderDetailModel>,var mactivity: OrderDetail2Activity) :
        BaseQuickAdapter<OrderDetailModel, BaseDataBindingHolder<ItemOrderdetailShop3adapterBinding>>(R.layout.item_orderdetail_shop3adapter, list) {


    override fun convert(helper:  BaseDataBindingHolder<ItemOrderdetailShop3adapterBinding>, item: OrderDetailModel) {
        val itemOrderdetailShop3adapterBinding = helper.dataBinding!!
        itemOrderdetailShop3adapterBinding.data = item

        val goodsRecyclerView = helper.getView<RecyclerView>(R.id.rv_orderplace_shop_goods)
        var absShopGoodsNum = 0
        if (item.order_sku_list != null) {
            absShopGoodsNum = item.order_sku_list!!.size
        }
        val goodsAdapter = OrderDetailGoods3Adapter(item.order_sku_list ?: arrayListOf(),mactivity)
        goodsRecyclerView.layoutManager = LinearLayoutManager(context)
        goodsRecyclerView.adapter = goodsAdapter

        //商品数量
        val tv_orderplace_shop_goodscn = helper.getView<TextView>(R.id.tv_orderplace_shop_goodscn)
        val goodsCnText = String.format(ResString.get(R.string.共_数_件商品), "${absShopGoodsNum}")
        val spannableString = StringUtils.setTextColor(goodsCnText, "${absShopGoodsNum}", ContextCompat.getColor(context, R.color.common_text_blue))
        tv_orderplace_shop_goodscn.setText(spannableString)

        helper.getView<TextView>(R.id.tv_orderplace_shopname).setOnClickListener {
            startShopActivity(context, item.seller_id)
        }
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            val absGoodsModel = adapter.getItem(position) as AbsGoodsModel
            startGoodsDetailActivity(context, absGoodsModel.absGoodsId)
        }

    }

}